package cn.com.jslh.boarpro.util.template;

import android.content.Context;
import android.os.Bundle;
import android.widget.Toast;

import com.blankj.utilcode.util.SPUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import cn.com.jslh.boarpro.base.BaseApplication;
import cn.com.jslh.boarpro.util.WebService;
import cn.com.jslh.boarpro.util.database.DataBaseUtil;

/**
 * Created by Administrator on 2017/11/27.
 */

public class TempNet {
    private JSONObject jsonObject;
    private Context context;
    private WebService.NetSuccessListener netSuccessListener;
    private String actionName;
    private String keyid;

    public TempNet() {
    }

    public void doAction() {
        try {
            BaseAction baseAction = (BaseAction) DataBaseUtil.GetOneData(context, BaseAction.class, " where actionName = '" + actionName + "'");
            if (baseAction == null) {
                Toast.makeText(context, "方法配置错误", Toast.LENGTH_SHORT).show();
                return;
            }
            JSONArray keys = new JSONArray(baseAction.getPostKeys());
            for (int i = 0; i < keys.length(); i++) {
                String pkey = keys.get(i).toString();
                if (jsonObject.has(pkey)) {
                    continue;
                }
                switch (pkey) {
                    case "pageid":
                        jsonObject.put(pkey, SPUtils.getInstance().getString(BaseApplication.MODULETYPE));
                        break;
                    case "lineid":
                        jsonObject.put(pkey, SPUtils.getInstance().getString(BaseApplication.FLINEID));
                        break;
                    case "userid":
                        jsonObject.put(pkey, SPUtils.getInstance().getString(BaseApplication.FUSERID));
                        break;
                    case "keyid":
                        if (keyid != null){
                            jsonObject.put(pkey,keyid);
                            break;
                        }
                        //从数据库中获取result 不能通过actionName 找 Keyid 因为Action可能相同
                        AddTempBaseItem addTempBaseItem0 = (AddTempBaseItem) DataBaseUtil.GetOneData(context,
                                AddTempBaseItem.class,
                                " where config like '%\":\"" + actionName + "\",%'");
                        if (addTempBaseItem0 != null) {
                            jsonObject.put(pkey, addTempBaseItem0.getKey());
                        }
                        break;
                    default:
                        //从数据库中获取result
                        AddTempBaseItem addTempBaseItem = (AddTempBaseItem) DataBaseUtil.GetOneData(context,
                                AddTempBaseItem.class,
                                " where key = '" + pkey + "'");
                        if (addTempBaseItem != null) {
                            jsonObject.put(pkey, addTempBaseItem.getResult());
                        }
                        break;
                }
            }
            new WebService.Builder(context, baseAction.getActionName(), baseAction.getSpace())
                    .addProperty("jsonString", jsonObject.toString())
                    .success(netSuccessListener)
                    .showDialog(true)
                    .post();


        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public static class Builder {
        private JSONObject jsonObject;
        private Context context;
        private WebService.NetSuccessListener netSuccessListener;
        private String actionName;
        private String keyid;

        public Builder(Context context) {
            jsonObject = new JSONObject();
            this.context = context;
        }

        public Builder addParam(String key, String data) {
            try {
                jsonObject.put(key, data);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return this;
        }

        public Builder setNetLitener(WebService.NetSuccessListener netLitener) {
            this.netSuccessListener = netLitener;
            return this;
        }

        public Builder setActionName(String actionName) {
            this.actionName = actionName;
            return this;
        }

        public Builder setKeyId(String keyId){
            this.keyid = keyId;
            return this;
        }

        public TempNet build() {
            TempNet tempNet = new TempNet();
            tempNet.jsonObject = this.jsonObject;
            tempNet.context = this.context;
            tempNet.netSuccessListener = netSuccessListener;
            tempNet.actionName = actionName;
            tempNet.keyid = keyid;
            return tempNet;
        }

    }
}
